package edu.gatech.bsbmule.presenter;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import edu.gatech.bsbmule.model.CustomizedTimer;
import edu.gatech.bsbmule.model.Game;
import edu.gatech.bsbmule.model.Map;
import edu.gatech.bsbmule.util.SaveAndLoadManager;
import edu.gatech.bsbmule.view.IWelcomeScreen;
import edu.gatech.bsbmule.view.MainWindow;
import edu.gatech.bsbmule.view.MapScreen;

/**
 * Presenter for welcome screen
 * 
 * @author BSB
 * @version 1.0 10/26/2013
 */
public class WelcomeScreenPresenter {

	private IWelcomeScreen view;

	public WelcomeScreenPresenter(IWelcomeScreen view) {
		this.view = view;
	}
	
	/**
	 * Setup listener for New Game button
	 * @param nextJPanel
	 */
	public void next(final JPanel nextJPanel) {
		view.setPlayButtonListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				MainWindow.getInstance().setPanel(nextJPanel);
			}
		});
	}
	
	/**
	 * Setup listener for Load button
	 * @param parent parent component of displaying JFileChooser
	 */
	public void setLoadButton(final JPanel parent) {
		view.setLoadButtonListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				// Create file chooser and extension filter
				final JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter filter
					= new FileNameExtensionFilter("json file", "json");
				chooser.addChoosableFileFilter(filter);
				chooser.setFileFilter(filter);
				
				// If load from file approved
				int returnVal = chooser.showOpenDialog(parent);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					BufferedReader reader = null;
					try
					{
						
						//Build up Json string and hand it to S/LManager
						File file = chooser.getSelectedFile();
						FileReader fr = new FileReader(file);
						reader = new BufferedReader(fr);
						String s;
						StringBuilder json = new StringBuilder();
						while ((s = reader.readLine()) != null) {
							json.append(s);
							json.append("\n");
						}
						SaveAndLoadManager.load(json.toString());
						
						// New msp
						MapScreenPresenter msp = new MapScreenPresenter(MapScreen.getInstance());
						Map map = Map.getInstance();
						
						// Setup players
						Game.getInstance().setTurnOrder();
						System.out.println(Game.getInstance());
						
						// Display tiles in map screen
						msp.displayTiles(map);
						msp.setTileButtonListener(map);
						
						// Shows the current player's information on the right player panel
						msp.updatePlayerInfo(Game.getInstance().getCurrentPlayer());
						
						// Display map screen, game resumes
						MainWindow.getInstance().setPanel(MapScreen.getInstance());
						MainWindow.getInstance().setPauseItemEnable(true);
						MainWindow.getInstance().setSaveItemEnable(true);
												
						// Timer kicks off
						CustomizedTimer myTimer = CustomizedTimer.getInstance(); 
						myTimer.setMapScreenPresenter(msp);
						myTimer.start();
						msp.startOneSecTimer();
						
					} catch (IOException e1) {
						System.err.println("Error loading file");
					} finally {
						if (reader != null) {
							try {
								reader.close();
							} catch (IOException e2) {
								System.err.println("Error loading file");
							}
						}
					}
				}
			}
		});
		
	}
}
